package adapletplugin.dialogs;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class LoginDialog extends TitleAreaDialog
{
	private String fHost;
	private String fUser;
	private String fPass;

	private Text fHostText;
	private Text fUserText;
	private Text fPassText;

	public LoginDialog(Shell parentShell)
	{
		super(parentShell);

		fHost = "";
		fUser = "";
		fPass = "";
	}

	@Override
	public void create()
	{
		super.create();

		setTitle("Login");
		setMessage("Please enter your login credentials");
		this.getButton(IDialogConstants.OK_ID).setEnabled(false);
	}

	@Override
	protected Control createDialogArea(Composite parent)
	{
		GridLayout layout = new GridLayout();
		parent.setLayout(layout);

		Composite composite = new Composite(parent, SWT.NULL);
		layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		Label label = new Label(composite, SWT.NULL);
		label.setText("Host");

		fHostText = new Text(composite, SWT.BORDER);
		fHostText.setText("");
		fHostText.addModifyListener(createKeyListener());
		fHostText.setLayoutData(new GridData(SWT.FILL, SWT.NULL, true, false));

		label = new Label(composite, SWT.NULL);
		label.setText("Username");

		fUserText = new Text(composite, SWT.BORDER);
		fUserText.setText("");
		fUserText.addModifyListener(createKeyListener());
		fUserText.setLayoutData(new GridData(SWT.FILL, SWT.NULL, true, false));

		label = new Label(composite, SWT.NULL);
		label.setText("Password");

		fPassText = new Text(composite, SWT.BORDER | SWT.PASSWORD);
		fPassText.setText("");
		fPassText.addModifyListener(createKeyListener());
		fPassText.setLayoutData(new GridData(SWT.FILL, SWT.NULL, true, false));

		return parent;
	}

	private ModifyListener createKeyListener()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (fHostText.getText().isEmpty())
				{
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}
				else if (fUserText.getText().isEmpty())
				{
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}
				// else if(fPassText.getText().isEmpty())
				// {
				// //getButton(IDialogConstants.OK_ID).setEnabled(false);
				// }
				else
				{
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		};
	}

	@Override
	protected void okPressed()
	{
		fHost = fHostText.getText();
		fUser = fUserText.getText();
		fPass = fPassText.getText();
		super.okPressed();
	}

	public String getHost()
	{
		return fHost;
	}

	public String getUser()
	{
		return fUser;
	}

	public String getPass()
	{
		return fPass;
	}

}
